import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * financialCode - 财务代码
 * @date: 2019-3-7
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { queryList, saveCreate as _saveCreate, saveUpdate as _saveUpdate } from "../services/financialCodeService";
export default {
  namespace: 'financialCode',
  state: {
    dataSource: [],
    // 表格数据源
    pagination: {},
    // 表格分页信息
    typeList: [] // 类型值集

  },
  effects: {
    // 获取类型值集
    fetchValue: /*#__PURE__*/_regeneratorRuntime.mark(function fetchValue(_ref, _ref2) {
      var payload, call, put, valueType, res;
      return _regeneratorRuntime.wrap(function fetchValue$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              valueType = payload.valueType;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryIdpValue, valueType);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  typeList: res
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchValue);
    }),
    // 获取表格数据
    fetchList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchList(_ref3, _ref4) {
      var payload, call, put, res, markChildren, markData;
      return _regeneratorRuntime.wrap(function fetchList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(queryList, payload);

            case 5:
              _context2.t1 = _context2.sent;
              res = (0, _context2.t0)(_context2.t1);

              markChildren = function markChildren() {
                var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
                var parentEnableFlag = arguments.length > 1 ? arguments[1] : undefined;
                return array.map(function (item) {
                  var data = _objectSpread(_objectSpread({}, item), {}, {
                    parentEnableFlag: parentEnableFlag
                  });

                  if (Array.isArray(data.children) && data.children.length) {
                    var parentEnable = data.enabledFlag;
                    data.children = markChildren(data.children, parentEnable);
                  }

                  return data;
                });
              };

              if (!res) {
                _context2.next = 12;
                break;
              }

              markData = markChildren(res);
              _context2.next = 12;
              return put({
                type: 'updateState',
                payload: {
                  dataSource: markData,
                  pagination: createPagination(res)
                }
              });

            case 12:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchList);
    }),
    // 保存新增财务代码
    saveCreate: /*#__PURE__*/_regeneratorRuntime.mark(function saveCreate(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveCreate$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(_saveCreate, payload);

            case 5:
              _context3.t1 = _context3.sent;
              res = (0, _context3.t0)(_context3.t1);
              return _context3.abrupt("return", res);

            case 8:
            case "end":
              return _context3.stop();
          }
        }
      }, saveCreate);
    }),
    // 保存更新的财务代码
    saveUpdate: /*#__PURE__*/_regeneratorRuntime.mark(function saveUpdate(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveUpdate$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.t0 = getResponse;
              _context4.next = 5;
              return call(_saveUpdate, payload);

            case 5:
              _context4.t1 = _context4.sent;
              res = (0, _context4.t0)(_context4.t1);
              return _context4.abrupt("return", res);

            case 8:
            case "end":
              return _context4.stop();
          }
        }
      }, saveUpdate);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref9) {
      var payload = _ref9.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};